﻿using Authentication.Core.Configuration;
using Authentication.Model.Interface;
using Authentication.Model.Model;
using Common.Contract;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Data.Repository.Enum;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class UpdateStatusListRequestDaoServiceCommand : ProviderServiceCommand<IProviderFactory, UpdateCreateDaoRequestRequest, ResponseRecord>
    {
        public UpdateStatusListRequestDaoServiceCommand(IProviderFactory providers) : base(providers) { }
        #region Private Properties
        private UserRecord currentUser;
        #endregion
        protected override void Validate(UpdateCreateDaoRequestRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
            if (request.ListCreateDaoRequest == null || request.ListCreateDaoRequest.Count == 0)
            {
                throw new ArgumentException("List record must be set", "request");
            }
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var record = Request.Record;
            bool savedUser = false;
            try
            {
                if (SystemConfiguration.UserBICApprovedDao.Contains(currentUser.UserName))
                {
                    var listId = Request.ListCreateDaoRequest.Select(x => x.RequestId).ToArray();
                    int status = Request.ListCreateDaoRequest.FirstOrDefault().StatusCreateDaoRequest;
                    savedUser = Providers.RequestDao.UpdateStatusListCreateDaoRequest(listId, status);
                }
                else
                {
                    response.ErrorMessage = string.Format("User {0} not have permission", currentUser.UserName);
                }
                //bool savedUser = Providers.RequestDao.SaveDaoRequest(userQuestion);
                //response.IsSuccess = savedUser;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = ex.Message;
            }
            return savedUser;
        }
    }
}
